package com.ruoyi.web.controller.inspection;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.dto.InspectionPlanTaskCountDTO;
import com.ruoyi.system.domain.vo.InspectionPlanTaskCountVO;
import com.ruoyi.system.domain.vo.InspectionPlanTaskDetailsCountVO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskCountVO;
import com.ruoyi.system.entity.InspectionPlanTask;
import com.ruoyi.system.service.InspectionPlanTaskDetailsService;
import com.ruoyi.system.service.InspectionPlanTaskService;
import com.ruoyi.system.service.InspectionPointService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * 巡检统计
 * @since 2022-11-28 17:55:24
 */
@RestController
@RequestMapping("/inspectionPlanTaskCount")
public class InspectionPlanTaskCountController extends BaseController {
    @Resource
    private InspectionPlanTaskService inspectionPlanTaskService;
    @Resource
    private InspectionPlanTaskDetailsService inspectionPlanTaskDetailsService;
    @Resource
    private InspectionPointService inspectionPointService;

    /**
     * 统计_正常巡检、超时巡检
     */
    @GetMapping("/countAll")
    public AjaxResult countAll() {
        Map<String, Object> map = new HashMap<>();
        int zcxj = inspectionPlanTaskService.getZCxj();
        int csxj = inspectionPlanTaskService.getCSxj();
        map.put("正常巡检",zcxj);
        map.put("超时巡检",csxj);
        return AjaxResult.success(map);
    }

    /**
     * 统计_按日、月统计巡检完成度（0按日、1按月）
     */
    @PostMapping("/countRy")
    public AjaxResult countRy(@RequestBody InspectionPlanTaskDetailsCountVO vo) {
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtils.isNotEmpty(vo)){
            if (StringUtils.isNotEmpty(vo.getDay())){
                int arxj = inspectionPlanTaskDetailsService.getARxj(vo.getDay());
                map.put("按日统计巡检完成度",arxj);
            }else if (StringUtils.isNotEmpty(vo.getMonth())){
                int ayxj = inspectionPlanTaskDetailsService.getAYxj(vo.getMonth());
                map.put("按月统计巡检完成度",ayxj);
            }else {
                int allxj = inspectionPlanTaskDetailsService.getAllxj();
                map.put("巡检完成度",allxj);
            }
        }
        return AjaxResult.success(map);
    }

    /**
     * 统计巡检完成度 正常巡检、超时巡检、按日、月日月任务完成情况
     */
    @GetMapping("/countAllRy")
    public AjaxResult countAllRy(Integer tempType) {
        Calendar now = Calendar.getInstance();
        String yearTemp = String.valueOf(now.get(Calendar.YEAR));//年
        String monthTemp = String.valueOf(now.get(Calendar.MONTH) + 1);//月
        String dayTemp = String.valueOf(now.get(Calendar.DAY_OF_MONTH));//日
        if (monthTemp.length() <2){
            monthTemp = "0" + monthTemp;
        }
        List<PreventionCheckTaskCountVO> allTask =new ArrayList<>();
        if (1 == tempType){// 按日统计
            allTask = inspectionPlanTaskDetailsService.getTaskDay(yearTemp + "-" + monthTemp + "-" + dayTemp);
        }else if (2 == tempType){// 按月统计
            allTask = inspectionPlanTaskDetailsService.getTaskMonth(yearTemp + "-" + monthTemp);
        }else {// 全部
            allTask = inspectionPlanTaskDetailsService.getAllTask();
        }
        return AjaxResult.success(allTask);
    }

    /** ===========================================================================
     * 统计_按路线
     */
    @GetMapping("/countRoute")
    public AjaxResult countRoute(InspectionPlanTaskCountDTO dto) {
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtils.isNotEmpty(dto)){
            if (0 == dto.getType()){
                List<InspectionPlanTaskCountVO> routeList = inspectionPlanTaskService.getAllRouteTo();
                map.put("按路线统计",routeList);
            }else if (1 == dto.getType()){
                int route = inspectionPlanTaskService.getCountRouteTo(dto.getName());
                map.put("按路线统计",route);
            }
        }
        return AjaxResult.success(map);
    }

    /**
     * 统计_按时间
     */
    @GetMapping("/countTime")
    public AjaxResult countTime(String startTime, String endTime) {
        List<InspectionPlanTask> list = inspectionPlanTaskService.getAllCountTimeTo(startTime,endTime);
        return AjaxResult.success(list);
    }

    /**
     * 统计_按区域
     */
    @GetMapping("/countArea")
    public AjaxResult countArea() {
        List<InspectionPlanTaskCountVO> list = inspectionPlanTaskService.getCountArea();
        return AjaxResult.success(list);
    }

    /**
     * 统计_按部门
     */
    @GetMapping("/countDept")
    public AjaxResult countDept() {
        List<InspectionPlanTaskCountVO> list = inspectionPlanTaskService.getCountDept();
        return AjaxResult.success(list);
    }

    /**
     * 统计_按巡检类型统计(未排查、已排查、超时未排查) Integer type,String name
     */
    @GetMapping("/countType")
    public AjaxResult countType() {
        List<InspectionPlanTaskCountVO>  allCountOne = inspectionPlanTaskService.getAllTypeCount();
        return AjaxResult.success(allCountOne);
    }

    /**
     * 统计_按路线统计发现隐患数量
     */
    @GetMapping("/countRouteDanger")
    public AjaxResult countRouteDanger() {
        List<InspectionPlanTaskCountVO> dangerList = inspectionPlanTaskService.getAllDangerList();
        return AjaxResult.success(dangerList);
    }


}

